// 我机
function Plane() {
    this.init = function() {
        // 我机的宽高
        this.width = 120;
        this.height = 79;

        // 我机在canvas上的坐标
        this.x = (canvas.width - this.width) / 2;;
        this.y = canvas.height - this.height - 50;

        // 绘制我机
        this.img = Tool.getImage('./images/myplane.png');

        // 我机飞行的方向
        this.isLeft = false;
        this.isUp = false;
        this.isRight = false;
        this.isDown = false;

        // 我机移动速度
        this.moveX = 5;
        this.moveY = 5;

        // 是否显示
        this.isShow = true;
    };
    this.init();

    this.draw = function() {
        if (this.isShow) {
            // 移动
            this.move();

            // 绘制
            ctx.drawImage(this.img, this.x, this.y);
        }
    };

    this.move = function() {
        // 左移动
        if (this.isLeft) {
            this.x -= this.moveX;
            // 上移动
        }

        if (this.isUp) {
            this.y -= this.moveY;
            // 右移动
        }

        if (this.isRight) {
            this.x += this.moveX;
            // 下移动
        }
        if (this.isDown) {
            this.y += this.moveY;
            // 左上移动
        }

        // 判断界限
        if (this.x < 0) this.x = 0;
        if (this.y < 0) this.y = 0;
        if (this.x > canvas.width - this.width) this.x = canvas.width - this.width;
        if (this.y > canvas.height - this.height) this.y = canvas.height - this.height;
    };

    this.keydown = function(e) {
        switch (e.keyCode) {
            case 37:
                this.isLeft = true;
                break;
            case 38:
                this.isUp = true;
                break;
            case 39:
                this.isRight = true;
                break;
            case 40:
                this.isDown = true;
                break;
        }
    };

    this.keyup = function(e) {
        switch (e.keyCode) {
            case 37:
                this.isLeft = false;
                break;
            case 38:
                this.isUp = false;
                break;
            case 39:
                this.isRight = false;
                break;
            case 40:
                this.isDown = false;
                break;

        }
    }
}